package com.laiketui.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laiketui.order.api.dto.MchOrderDTO;
import com.laiketui.order.api.dto.OrderDetailDTO;
import com.laiketui.order.api.dto.OrderMessageDTO;
import com.laiketui.order.api.dto.StoreOrderDTO;
import com.laiketui.order.api.page.MyPage;
import com.laiketui.order.api.params.store.StoreOrderDetailParams;
import com.laiketui.order.api.params.store.StoreOrderInfoParams;
import com.laiketui.order.api.params.store.StoreOrderListParams;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderDetail;
import com.laiketui.order.domain.OrderMessage;
import com.laiketui.order.domain.OrderParentMessage;
import com.laiketui.order.entity.StoreOrderEntity;
import com.laiketui.order.mapper.OrderMapper;
import com.laiketui.order.mapstruct.OrderDetailMapstructMapper;
import com.laiketui.order.mapstruct.OrderMessageMapstructMapper;
import com.laiketui.order.mapstruct.StoreOrderMapstructMapper;
import com.laiketui.order.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class StoreOrderServiceImpl implements StoreOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderMessageService orderMessageService;
    @Autowired
    private OrderParentMessageService orderParentMessageService;

    @Override
    public MyPage<StoreOrderDTO> getOrderList(StoreOrderListParams params) {
        Page<StoreOrderEntity> page = new Page<>(params.getCurrent(), params.getPageSize());

        Page<StoreOrderEntity> storeOrderEntityPage = orderMapper.selecOrderByPage(page,params);

        List<StoreOrderDTO> dtOs = StoreOrderMapstructMapper.INSTANCE.toDTOs(storeOrderEntityPage.getRecords());

        return new MyPage<StoreOrderDTO>(storeOrderEntityPage.getCurrent(), storeOrderEntityPage.getSize(), storeOrderEntityPage.getTotal(), dtOs);
    }

    @Override
    public StoreOrderDTO getOrderInfo(StoreOrderInfoParams params) {
        StoreOrderEntity order = orderMapper.getOrderByOrderNo(params.getOrderNo());
        StoreOrderDTO storeOrderDTO = StoreOrderMapstructMapper.INSTANCE.convert(order);
        storeOrderDTO.setFactoryName(warehouseService.getFactoryName(params.getOrderNo()));

        List<OrderDetail>details=orderDetailService.findByOrderNo(params.getOrderNo());
        List<OrderDetailDTO> dtOs = OrderDetailMapstructMapper.INSTANCE.toDTOs(details);
        storeOrderDTO.setDetails(dtOs);

        //获取是否需要质检
        boolean needQualityInspection = warehouseService.getNeedQualityInspection(order.getMchId());
        if(needQualityInspection){
            storeOrderDTO.setIsNeedQualityInspection(1);
        }else{
            storeOrderDTO.setIsNeedQualityInspection(0);
        }

        //获取子订单消息
        List<OrderMessage> messages=orderMessageService.listByOrderNo(order.getOrderNo());
        List<OrderMessageDTO> dtoMessages = OrderMessageMapstructMapper.INSTANCE.toDTOs(messages);

        //将母单消息也加入子单消息
        addOrderParentMessage2OrderMessages(order, dtoMessages);
        storeOrderDTO.setMessages(dtoMessages);

        return storeOrderDTO;
    }

    private void addOrderParentMessage2OrderMessages(StoreOrderEntity order, List<OrderMessageDTO> dtoMessages) {
        List<OrderParentMessage> orderParentMessages =orderParentMessageService.listByOrderParentNo(order.getOrderParentNo());
        for (OrderParentMessage orderParentMessage : orderParentMessages) {
            OrderMessageDTO orderMessageDTO = new OrderMessageDTO();
            orderMessageDTO.setOrderNo(order.getOrderNo());
            orderMessageDTO.setOrderParentNo(order.getOrderParentNo());
            orderMessageDTO.setCreateBy(order.getCreateBy());
            orderMessageDTO.setCreateTime(order.getCreateTime());
            orderMessageDTO.setMessage(orderParentMessage.getMessageContent());
            /**
             * 母单消息以5开头加上 {@link com.laiketui.order.api.enums.OrderParentMessageType}
             */
            orderMessageDTO.setMessageType(500+orderParentMessage.getMessageType());
            orderMessageDTO.setRemark(orderParentMessage.getRemark());
            orderMessageDTO.setPhoto(orderParentMessage.getPhoto());
            orderMessageDTO.setStoreId(order.getStoreId());
            dtoMessages.add(orderMessageDTO);
        }
    }
}
